package tech.yunjing.train.util;


import tech.yunjing.train.entity.SmallRedPacket;

import java.math.BigDecimal;
import java.util.*;

/**
 * .
 * 该类是实用工具类,用于存放各种静态方法供其它类调用
 * Created by 刘飞 on 2017-6-8
 */
public class Utility {




    /**
     *
     * @param total  红包中的最大金额
     * @param num    红包中小红包的数量   0.01表示红包的最小金额
     * @return    map 其中存放着红包的id和红包的金额大小
     */

    public static  Map<Integer,Double> getSmallRedPacket(double total, int num) {
        Map<Integer,Double>map = new HashMap<Integer,Double>();

        for(int i=1;i<num;i++){

            double safe_total=(total-(num-i)*0.01)/(num-i);

            double money=Math.random()*(safe_total-0.01)+0.01;

            BigDecimal money_bd=new BigDecimal(money);

            money=money_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

            total=total-money;

            BigDecimal total_bd=new BigDecimal(total);

            total=total_bd.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

            map.put(i,money);
        }
        map.put(num,total);
        return map;
    }

    /**
     * 根据Calendar来修改日期 获得停止时间
     * @return
     */
    public static Date getStopDate(){
        Date date=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.DAY_OF_WEEK,1);
        return calendar.getTime();
    }
    public static Double getTotal(List<SmallRedPacket> list){
        double total=0;
        for(SmallRedPacket smallRedPacket:list){
            if(smallRedPacket.getStatus()==0){
                total+=smallRedPacket.getSmallRedPacketAmount();
            }
        }
        return total;
    }
    public static int getSmallRedPacketNumber(List<SmallRedPacket> list){
        int smallRedPacketNumber=0;
        for(SmallRedPacket smallRedPacket:list){
            if(smallRedPacket.getStatus()==0){
                smallRedPacketNumber++;

            }
        }
        return smallRedPacketNumber;
    }







}
